#!/bin/bash
# /********************************************************************************
# * Copyright (c) 2022 Contributors to the Eclipse Foundation
# *
# * See the NOTICE file(s) distributed with this work for additional
# * information regarding copyright ownership.
# *
# * This program and the accompanying materials are made available under the
# * terms of the Apache License 2.0 which is available at
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * SPDX-License-Identifier: Apache-2.0
# ********************************************************************************/
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export SCRIPT_DIR

CONFIG_JSON="$SCRIPT_DIR/../prerequisite_settings.json"

# get databroker, feeder tags from prerequisite_settings.json
FEEDER_TAG=$(jq -r '.feedercan.imageVersion // empty' "$CONFIG_JSON")
KDB_TAG=$(jq -r '.databroker.imageVersion // empty' "$CONFIG_JSON")
# default values
[ -z "$KDB_TAG" ] && export KDB_TAG="v0.17.0"
[ -z "$FEEDER_TAG" ] && export FEEDER_TAG="v0.1.0"

# allow custom tags from CI
[ -z "$SEAT_TAG" ] && export SEAT_TAG="v0.1.0"
[ -z "$HVAC_TAG" ] && export HVAC_TAG="v0.1.0"

### Local docker image config (may force rebuild + tag local images)
# export HVAC_TAG="prerelease"
# export SEAT_TAG="prerelease"

### ghcr.io image config
export DOCKER_REPO="ghcr.io"
export KDB_IMAGE="${DOCKER_REPO}/eclipse/kuksa.val/databroker:${KDB_TAG}"
export FEEDER_IMAGE="${DOCKER_REPO}/eclipse/kuksa.val.feeders/dbc2val:${FEEDER_TAG}"
export SEAT_IMAGE="${DOCKER_REPO}/eclipse/kuksa.val.services/seat_service:${SEAT_TAG}"
export HVAC_IMAGE="${DOCKER_REPO}/eclipse/kuksa.val.services/hvac_service:${HVAC_TAG}"

export KDB_CONTAINER="it-databroker"
export SEAT_CONTAINER="it-seat_service"
export HVAC_CONTAINER="it-hvac_service"
export FEEDER_CONTAINER="it-feedercan"

KDB_HOST_PORT="35555"
SEAT_HOST_PORT="35051"
HVAC_HOST_PORT="35052"
DOCKER_KDB_PORT="55555"
DOCKER_KDB_ADDRESS="${KDB_CONTAINER}:$DOCKER_KDB_PORT"

### dbc2val container setup. Have to map $FEEDER_HOST_DIR into $FEEDER_MOUNT_DIR in container and setup dbcfeeder to use custom config in databroker usecase
FEEDER_HOST_DIR="$SCRIPT_DIR/volumes/dbc2val"
FEEDER_MOUNT_DIR="/conf"
# FEEDER_MOUNT_OPT="--mount type=bind,src=$FEEDER_HOST_DIR,dst=${FEEDER_MOUNT_DIR},readonly"
FEEDER_MOUNT_OPT="-v $FEEDER_HOST_DIR:${FEEDER_MOUNT_DIR}"
# NOTE: for [dbc2val:v0.1.0] FEEDER_ENV="-e DBC_FILE=$FEEDER_MOUNT_DIR/it-can.dbc -e MAPPING_FILE=$FEEDER_MOUNT_DIR/it-mapping.yml -e CANDUMP_FILE=$FEEDER_MOUNT_DIR/it-candump0.log -e USECASE=databroker"
FEEDER_ENV="-e DBC_FILE=$FEEDER_MOUNT_DIR/it-can.dbc -e MAPPING_FILE=$FEEDER_MOUNT_DIR/it-vss_4.0.json -e CANDUMP_FILE=$FEEDER_MOUNT_DIR/it-candump0.log -e USE_DBC2VAL=1"
FEEDER_LOG="info" #,dbcfeeder=debug,kuksa_client.grpc=debug,dbcfeederlib.canplayer=debug,dbcfeederlib.canreader=debug,dbcfeederlib.dbc2vssmapper=debug"

export KDB_DOCKER_OPT="-p   ${KDB_HOST_PORT}:55555/tcp --expose ${KDB_HOST_PORT}  -e RUST_LOG=info,databroker=debug,vehicle_data_broker=debug -e VEHICLEDATABROKER_DAPR_APP_ID=kdb --name ${KDB_CONTAINER}"
export SEAT_DOCKER_OPT="-p ${SEAT_HOST_PORT}:50051/tcp --expose ${SEAT_HOST_PORT} -e BROKER_ADDR=${DOCKER_KDB_ADDRESS} -e SIM=cansim -e SC_RPM=100 -e VEHICLEDATABROKER_DAPR_APP_ID=kdb --name ${SEAT_CONTAINER}"
export HVAC_DOCKER_OPT="-p ${HVAC_HOST_PORT}:50052/tcp --expose ${HVAC_HOST_PORT} -e VDB_ADDRESS=${DOCKER_KDB_ADDRESS} -e VEHICLEDATABROKER_DAPR_APP_ID=kdb -e LOG_LEVEL=debug --name ${HVAC_CONTAINER}"
export FEEDER_DOCKER_OPT="-e KUKSA_ADDRESS=${KDB_CONTAINER} -e KUKSA_PORT=$DOCKER_KDB_PORT ${FEEDER_ENV} ${FEEDER_MOUNT_OPT} -e LOG_LEVEL=${FEEDER_LOG} --name ${FEEDER_CONTAINER}"

export DOCKER_NETWORK="val-int" # "host"

export DOCKER_OPT=""

# limit docker log output to 100 lines / 5 min
#export DOCKER_LOG="-n 100"
[ -z "${DOCKER_LOG}" ] && export DOCKER_LOG="--since 5m -n 1000"

# output dir with results and logs
export RESULTS="/tmp/val-it-results"
